package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.DistBatchReqVo;
import com.miplus.ccrm.core.controller.vo.conf.DistBatchResVo;
import com.miplus.ccrm.core.controller.vo.conf.DistBatchTailReqVo;
import com.miplus.ccrm.core.pojo.FiDistbatchHist;
import com.miplus.ccrm.core.mapper.FiDistbatchHistMapper;
import com.miplus.ccrm.core.pojo.FiVisit;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author pc
* @description 针对表【fi_distbatch_hist(分案批次历史记录表)】的数据库操作Service实现
* @createDate 2024-04-07 17:35:27
*/
@Service
public class FiDistbatchHistServiceImpl extends MPJBaseServiceImpl<FiDistbatchHistMapper, FiDistbatchHist>
    implements FiDistbatchHistService{

    @Override
    public IPage<DistBatchResVo> distBatchList(DistBatchReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                DistBatchResVo.class,
                new MPJLambdaWrapper<FiDistbatchHist>()
                        .selectAs("t.batchid",DistBatchResVo::getDistbatchid)
                        .selectAs("t.batchname",DistBatchResVo::getDistbatchname)
                        .selectAs("fun_getnames('PUBLIC.MORAUTO',t.actiontype)",DistBatchResVo::getDisttypedsc)
                        .selectAs("t.actiondt",DistBatchResVo::getDistdt)
                        .selectAs("hsum.okcnt",DistBatchResVo::getDistcnt)
                        .selectAs("hsum.distamt",DistBatchResVo::getDistamt)
                        .selectAs("t.actionusr",DistBatchResVo::getDistusr)
                        .selectAs("date_add(t.actiondt,interval t.binddays day)",DistBatchResVo::getBindenddt)
                        .selectAll(FiDistbatchHist.class)
                        .innerJoin("(select hg.batchid,sum(hg.okcnt) as okcnt,sum(hg.distamt) as distamt from fi_distbatch_hist hg group by hg.batchid) hsum on t.batchid=hsum.batchid")
                        .apply("t.id=(select min(id) from fi_distbatch_hist h3 where h3.batchid=t.batchid)")
                        .eq(JPubUtils.IsNotEmpty(paras.getBatchid()),FiDistbatchHist::getBatchid,paras.getBatchid())
                        .likeRight(JPubUtils.IsNotEmpty(paras.getBatchname()),FiDistbatchHist::getBatchname,paras.getBatchname())
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), FiDistbatchHist::getActiondt,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .apply(JPubUtils.IsEqual(paras.getExistcase(),"1"),"t.batchid in (select distinct fi.distbatchid from fi_creditcases fi)")
                        .orderByDesc(FiDistbatchHist::getActiondt)
        );
    }

    @Override
    public IPage<DistBatchResVo> distBatchDetail(DistBatchTailReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                DistBatchResVo.class,
                new MPJLambdaWrapper<FiDistbatchHist>()
                        .selectAs("t.batchid",DistBatchResVo::getDistbatchid)
                        .selectAs("t.distbatchname",DistBatchResVo::getDistbatchname)
                        .selectAs("fun_getnames('PUBLIC.MORAUTO',t.actiontype)",DistBatchResVo::getDisttypedsc)
                        .selectAs("t.actiondt",DistBatchResVo::getDistdt)
                        .selectAs("t.okcnt",DistBatchResVo::getDistcnt)
                        .selectAs("t.distamt",DistBatchResVo::getDistamt)
                        .selectAs("t.actionusr",DistBatchResVo::getDistusr)
                        .selectAs("date_add(t.actiondt,interval t.binddays day)",DistBatchResVo::getBindenddt)
                        .selectAll(FiDistbatchHist.class)
                        .eq(JPubUtils.IsNotEmpty(paras.getBatchid()),FiDistbatchHist::getBatchid,paras.getBatchid())
                        .orderByDesc(FiDistbatchHist::getActiondt)
        );
    }

    @Override
    public List<LabelResVo> getDistBatchList()
    {
        return selectJoinList(LabelResVo.class,
                new MPJLambdaWrapper<FiDistbatchHist>()
                        .selectAs("batchid",LabelResVo::getValue)
                        .selectAs("batchname",LabelResVo::getLabel)
                        .select("batchid","batchname")
                        .apply("t.batchid in (select distinct fi.distbatchid from fi_creditcases fi)")
                        .orderByDesc(FiDistbatchHist::getId)
        );
    }
}




